package com.user.test.view.activities

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.activity.viewModels
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.Fragment
import com.user.test.R
import com.user.test.TestApp
import com.user.test.databinding.ActivityMainBinding
import com.user.test.view.fragments.web.WebFragment
import com.user.test.view.fragments.searchResult.SearchResultFragment
import com.user.test.viewModel.RetrofitViewModel
import com.user.test.viewModel.RoomViewModel
import com.user.test.viewModel.ViewStateViewModel


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val retrofitViewModel : RetrofitViewModel by viewModels()// by lazy { ViewModelProvider(this).get(RetrofitViewModel::class.java) }
    private val roomViewModel : RoomViewModel by viewModels() //by lazy { ViewModelProvider(this).get(RoomViewModel::class.java) }
    private val viewStateViewModel : ViewStateViewModel by viewModels() //by lazy { ViewModelProvider(this).get(RoomViewModel::class.java) }



    @SuppressLint("WrongConstant")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)


        val webBundle = Bundle()
        val webFragment = WebFragment()

        replaceFragment(R.id.fragment_container, SearchResultFragment())
        retrofitViewModel.getLink().observe(this){
            webBundle.putString("link", it)
            webFragment.arguments = webBundle
            replaceFragment(R.id.fragment_container, webFragment)
        }

        retrofitViewModel.getError().observe(this){
            binding.progressBar.visibility = 8 // set progressBar.visibility GONE
            Toast.makeText(this, "Error: $it", Toast.LENGTH_LONG).show()
        }

        retrofitViewModel.getResponse().observe(this){
            binding.progressBar.visibility = 8 // set progressBar.visibility GONE
            if(it.isNotEmpty()) {
                println(it)
                roomViewModel.insertList(it.toList())
                viewStateViewModel.setHolderState(true)
            }
        }

        binding.apply {
            buttonForSearch.setOnClickListener {
                if(requestField.text.isNotEmpty()) {

                    hideKeyboard(binding.root)

                    viewStateViewModel.setHolderState(false)
                    progressBar.visibility = 0 // set progressBar.visibility Visible

                    retrofitViewModel.fetchSearchResultList(
                        (application as? TestApp)?.searchResultApi,
                        requestField.text.toString()
                    )
                }
            }
        }
    }

    private fun replaceFragment(containerViewId: Int, fragment:Fragment){
        supportFragmentManager
            .beginTransaction()
            .replace(containerViewId, fragment)
            .commit()
    }
    private fun hideKeyboard(view: View) {
        val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
}